<?php

class CustomerLocationAccountingController extends Controller {

    
     public function actionlistso ($custno,$locno, $query = NULL, $page = 1, $sort = NULL, $dir = NULL) {
        $criteria = new CDbCriteria();
        $criteria->condition = "custno =:custno and locno=:locno and invoicetype=:invoicetype and jobnumber<>''";
        $criteria->params = array('custno'=>$custno,
									'locno'=>$locno,
                                  'invoicetype'=> Invoice::TYPE_SALES_ORDERS);
        $criteria->order = "invoicedate ASC";

        if ($page > 0)
            $page--;
        $data = new CActiveDataProvider('Invoice', array(
                    'criteria' => $criteria,
                    'pagination' => array(
                        'pageSize' => 99999999,
                        'currentPage' => $page,
                    )
                ));
        $rs = array();
        if (count($data->getData()) > 0) {
            $rs['totalCount'] = $data->getTotalItemCount();
            $rs['type'] = 'Invoices';
           
            foreach ($data->getData() as $i => $item) {
                $rs['data'][$i] = $item->getAttributes();
             //   $rs['data'][$i]['amount'] = $item->getPaymentApplied();
                $rs['data'][$i]['amount'] = $item->amountcharge;
                $datetime1 = new DateTime($item->duedate);
                $datetime2 = new DateTime();
                $interval = $datetime1->diff($datetime2);
                $rs['data'][$i]['balance'] = $item->amountcharge - $item->getPaymentApplied();
                if($rs['data'][$i]['balance']<0) $rs['data'][$i]['balance'] =0;
                $rs['data'][$i]['aging'] = $interval->days;
                $refacc = $item->customer->quickbook . ':' . $item->jobnumber;
                $criteria = new CDbCriteria();
                $criteria->condition = "customerRefFullName='$refacc'";
                $criteria->order = "date Asc";
                $paymentsApplied = QbPaymentsapplied::model()->findAll($criteria);
                $paymentsString = '';
                foreach ($paymentsApplied as $payment){
                    $paymentsString.= "$payment->totalAmount - $payment->memo <br/>";
                }
                $rs['data'][$i]['deposits'] = $paymentsString;  
            }
            echo json_encode($rs);
        }
        Yii::app()->end();
    }

    
       public function actionlistcm ($custno,$locno, $query = NULL, $page = 1, $sort = NULL, $dir = NULL) {
        $criteria = new CDbCriteria();
        $criteria->condition = "custno =:custno and locno=:locno and invoicetype=:invoicetype";
        $criteria->params = array('custno'=>$custno,
									'locno'=>$locno,
                                  'invoicetype'=> Invoice::TYPE_CREDIT_MEMO);
        $criteria->order = "invoicedate ASC";

        if ($page > 0)
            $page--;
        $data = new CActiveDataProvider('Invoice', array(
                    'criteria' => $criteria,
                    'pagination' => array(
                        'pageSize' => 99999999,
                        'currentPage' => $page,
                    )
                ));
        $rs = array();
        if (count($data->getData()) > 0) {
            $rs['totalCount'] = $data->getTotalItemCount();
            $rs['type'] = 'Invoices';
           
            foreach ($data->getData() as $i => $item) {
                $rs['data'][$i] = $item->getAttributes();
             //   $rs['data'][$i]['amount'] = $item->getPaymentApplied();
                $rs['data'][$i]['amount'] = $item->amountcharge;
                $datetime1 = new DateTime($item->duedate);
                $datetime2 = new DateTime();
                $interval = $datetime1->diff($datetime2);
                $rs['data'][$i]['balance'] = $item->amountcharge;
                if($rs['data'][$i]['balance']<0) $rs['data'][$i]['balance'] =0;
                $rs['data'][$i]['aging'] = $interval->days;
            }
            echo json_encode($rs);
        }
        Yii::app()->end();
    }
 public function actionlistinv ($custno,$locno, $query = NULL, $page = 1, $sort = NULL, $dir = NULL) {
        $criteria = new CDbCriteria();
        $criteria->condition = "custno =:custno and locno=:locno and invoicetype=:invoicetype";
        $criteria->params = array('custno'=>$custno,
								   'locno'=>$locno,
                                  'invoicetype'=> Invoice::TYPE_INVOICE);
        $criteria->order = "invoicedate ASC";

        if ($page > 0)
            $page--;
        $data = new CActiveDataProvider('Invoice', array(
                    'criteria' => $criteria,
                    'pagination' => array(
                        'pageSize' => 99999999,
                        'currentPage' => $page,
                    )
                ));
        $rs = array();
        if (count($data->getData()) > 0) {
            $rs['totalCount'] = $data->getTotalItemCount();
            $rs['type'] = 'Invoices';
           
            foreach ($data->getData() as $i => $item) {
                $rs['data'][$i] = $item->getAttributes();
             //   $rs['data'][$i]['amount'] = $item->getPaymentApplied();
                $rs['data'][$i]['amount'] = $item->amountcharge;
                $datetime1 = new DateTime($item->duedate);
                $datetime2 = new DateTime();
                $interval = $datetime1->diff($datetime2);
                $rs['data'][$i]['balance'] = $item->amountcharge + $item->getPaymentApplied();
                if($rs['data'][$i]['balance']<0) $rs['data'][$i]['balance'] =0;
                $rs['data'][$i]['aging'] = $interval->days;
            }
            echo json_encode($rs);
        }
        Yii::app()->end();
    }


}
